package com.jing.ScaleImageView.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import androidx.appcompat.widget.AppCompatImageView;

//public class MyImageView extends AppCompatImageView {
//
//    private Matrix matrix = new Matrix();
//    private float lastX, lastY;
//
//    public MyImageView(Context context) {
//        this(context, null);
//    }
//
//    public MyImageView(Context context, AttributeSet attrs) {
//        this(context, attrs, 0);
//    }
//
//    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//        init(context);
//    }
//
//    private void init(Context context) {
//        setScaleType(ScaleType.MATRIX);
//    }
//
//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//    }
//
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        final int action = event.getActionMasked();
//        switch (action) {
//            case MotionEvent.ACTION_DOWN:
//                lastX = event.getX();
//                lastY = event.getY();
//                break;
//            case MotionEvent.ACTION_MOVE:
//                float dx = event.getX() - lastX;
//                float dy = event.getY() - lastY;
//                matrix.postTranslate(dx, dy);
//                setImageMatrix(matrix);
//                lastX = event.getX();
//                lastY = event.getY();
//        }
//        return true;
//    }
//}

//
//
//public class MyImageView extends AppCompatImageView {
//
//    private Matrix matrix = new Matrix();
//    private ScaleGestureDetector scaleDetector;
//    private float lastX, lastY;
//    private boolean allPointersUp;
//
//    public MyImageView(Context context) {
//        this(context, null);
//    }
//
//    public MyImageView(Context context, AttributeSet attrs) {
//        this(context, attrs, 0);
//    }
//
//    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//        init(context);
//    }
//
//    private void init(Context context) {
//        scaleDetector = new ScaleGestureDetector(context, new ScaleListener());
//        setScaleType(ScaleType.MATRIX);
//    }
//
//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//    }
//
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        scaleDetector.onTouchEvent(event);
//        final int action = event.getActionMasked();
//        switch (action) {
//            case MotionEvent.ACTION_DOWN:
//                lastX = event.getX();
//                lastY = event.getY();
//                allPointersUp = false;
//                break;
//            case MotionEvent.ACTION_POINTER_DOWN:
//                allPointersUp = true;
//                break;
//            case MotionEvent.ACTION_MOVE:
//                if (!allPointersUp) {
//                    float dx = event.getX() - lastX;
//                    float dy = event.getY() - lastY;
//                        matrix.postTranslate(dx, dy);
//                        setImageMatrix(matrix);
//                        lastX = event.getX();
//                        lastY = event.getY();
//                }
//                break;
//        }
//        return true;
//    }
//
//
//    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
//        @Override
//        public boolean onScale(ScaleGestureDetector detector) {
//            float scaleFactorChange = detector.getScaleFactor();
//            // 获取ImageView的中心点作为缩放中心点
//            float px = getWidth() / 2.0f;
//            float py = getHeight() / 2.0f;
//            // 在ImageView的中心点进行缩放
//            matrix.postScale(scaleFactorChange, scaleFactorChange, px, py);
//            // 应用新的矩阵变换
//            setImageMatrix(matrix);
//            return true;
//        }
//        @Override
//        public void onScaleEnd(ScaleGestureDetector detector) {
//            super.onScaleEnd(detector);
//        }
//    }
//}


public class MyImageView extends AppCompatImageView {

    private Matrix matrix = new Matrix();
    private ScaleGestureDetector scaleDetector;
    private float lastX, lastY;
    private boolean isDragging;
    private float d;
    private float newRot;

    public MyImageView(Context context) {
        this(context, null);
    }

    public MyImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        scaleDetector = new ScaleGestureDetector(context, new ScaleListener());
        setScaleType(ScaleType.MATRIX);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        scaleDetector.onTouchEvent(event);
        handleRotation(event);

        final int action = event.getActionMasked();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getX();
                lastY = event.getY();
                isDragging = true;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                isDragging = false;
                break;
            case MotionEvent.ACTION_MOVE:
                if (isDragging) {
                    float dx = event.getX() - lastX;
                    float dy = event.getY() - lastY;
                    matrix.postTranslate(dx, dy);
                    setImageMatrix(matrix);
                    lastX = event.getX();
                    lastY = event.getY();
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                isDragging = false;
                break;
        }
        return true;
    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            float scaleFactorChange = detector.getScaleFactor();
            float px = getWidth() / 2.0f;
            float py = getHeight() / 2.0f;
            matrix.postScale(scaleFactorChange, scaleFactorChange, px, py);
            setImageMatrix(matrix);
            return true;
        }
    }
    private void handleRotation(MotionEvent event) {
        if (event.getPointerCount() == 2) {
            if (event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
                d = rotation(event);
            } else if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
                newRot = rotation(event);
                matrix.postRotate(newRot - d, getWidth() / 2.0f, getHeight() / 2.0f);
                setImageMatrix(matrix);
                d = newRot;
            }
        }
    }

    private float rotation(MotionEvent event) {
        double delta_x = event.getX(1) - event.getX(0);
        double delta_y = event.getY(1) - event.getY(0);
        return (float) Math.toDegrees(Math.atan2(delta_y, delta_x));
    }
}
